package com.example.forum.mapper;

import com.example.forum.entity.Message;
import com.example.forum.vo.MessageVO;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

/**
 * @author lyk19
 * @description 针对表【t_message】的数据库操作Mapper
 * @createDate 2024-08-01 12:54:53
 * @Entity generator.domain.TMessage
 */

@Mapper
public interface MessageMapper {

    /**
     * 发送消息
     *
     * @param message
     * @return
     */
    @Insert("insert into t_message(receiveUserId, content, postUserId, createTime, updateTime, deleteState, state)" +
            "values(#{receiveUserId}, #{content}, #{postUserId}, #{createTime}, #{updateTime}, #{deleteState}, #{state})")
    Integer send(Message message);

    /**
     * 获取所有消息
     *
     * @param userId
     * @return
     */
    @Select("select * from t_message where receiveUserId = #{userId} and deleteState = 0")
    List<MessageVO> getAll(Long userId);

    /**
     * 获取未读消息数量
     *
     * @param userId
     * @return
     */
    @Select("select count(*) from t_message where receiveUserId = #{userId} and deleteState = 0 and state = 0")
    Integer getUnreadCount(Long userId);

    /**
     * 根据id获取消息
     *
     * @param id
     * @return
     */
    @Select("select * from t_message where id = #{id}")
    Message getById(Long id);

    /**
     * 标记消息已读
     *
     * @param id
     * @param userId
     * @return
     */
    @Update("update t_message set state = 1 where id = #{id} and receiveUserId = #{userId}")
    Integer markRead(Long id, Long userId);

    /**
     * 标记为已回复
     *
     * @param repliedId
     */
    @Update("update t_message set state = 2 where id = #{repliedId}")
    void replied(Long repliedId);
}
